﻿package sprites {
	import org.flixel.*;

	public class Player extends FlxSprite {
		[Embed(source="../../bin/imgs/pig.PNG")]
		private var ImgPlayer:Class;

		private var _move_speed:int = 400;
		private var _jump_power:int = 800;
		private var _max_health:int = 10;
		private var _hurt_counter:Number = 0;
		private var _energy_recover_count:Number;
		private var _offset:FlxPoint;
		public var weapon:Weapon;

		static private var E_RECOVER_COUNT:uint = 5;
		static private var E_RECOVER:uint = 10;

		public function Player(X:Number, Y:Number):void {
			super(X, Y, ImgPlayer); //Max speeds

			weapon = new Weapon(this);

			//origin = new FlxPoint(pixels.width, pixels.height);
			origin = new FlxPoint(0, 0);
			
			maxVelocity.x = 200;
			maxVelocity.y = 200;
			//Set the player health
			health = 10;
			_energy_recover_count = E_RECOVER_COUNT;
			//Gravity
			//acceleration.y = 420;            
			//Friction
			//angularDrag = 300;
			drag.y = drag.x = 100;

			//bounding box tweaks
			antialiasing = true;

			//addAnimation("normal", [0, 1, 2, 3], 10);
		/*
		   addAnimation("jump", [2]);
		   addAnimation("attack", [4,5,6],10);
		   addAnimation("stopped", [0]);
		   addAnimation("hurt", [2,7],10);
		   addAnimation("dead", [7, 7, 7], 5);
		 */
		}

		override public function update():void {

			if (dead){
				if (finished)
					exists = false;
				else
					super.update();
				return;
			}
			if (_hurt_counter > 0){
				_hurt_counter -= FlxG.elapsed * 3;
			}


			_energy_recover_count -= FlxG.elapsed;
			if (_energy_recover_count <= 0){
				_energy_recover_count = E_RECOVER_COUNT;
				weapon.recover(E_RECOVER);
			}


			if (FlxG.keys.LEFT){
				//facing = false;
				velocity.x -= _move_speed * FlxG.elapsed;
			}
			if (FlxG.keys.RIGHT){
				//facing = true;
				velocity.x += _move_speed * FlxG.elapsed;
			}
			if (FlxG.keys.UP){
				//facing = true;
				velocity.y -= _move_speed * FlxG.elapsed;
			}
			if (FlxG.keys.DOWN){
				//facing = true;
				velocity.y += _move_speed * FlxG.elapsed;
			}
			if (FlxG.keys.A){
				weapon.angle-=2;
			}
			if (FlxG.keys.D){
				weapon.angle+=2;
			}
			if (FlxG.keys.ONE) {
				weapon.recover(weapon._menergy);
			}
			if (FlxG.keys.Z){
				if (weapon._shoot_delay > 0){
					weapon._shoot_delay -= FlxG.elapsed;
				}
				weapon.at(this);
				weapon.shoot();
			}
			/*
			   if(FlxG.justPressed(FlxG.A) && velocity.y == 0)
			   {
			   velocity.y = -_jump_power;
			   }
			 */

			if (_hurt_counter > 0){
				//play("hurt");
			} else {
				if (velocity.y != 0){
					//play("jump");
				} else {
					if (velocity.x == 0){
						// play("stopped");
					} else {
						// play("normal");
					}
				}
			}

			super.update();
		}

		override public function hurt(Damage:Number):void {
			_hurt_counter = 1;
			return super.hurt(Damage);
		}
	}
}
